package com.immunology.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.immunology.domain.Role;
import com.immunology.domain.User;
import com.immunology.exception.UserAlreadyExistException;
import com.immunology.service.RoleService;
import com.immunology.service.UserService;
import com.immunology.service.security.UserSecurityService;

@RequestMapping("/users")
@Controller
public class UserController {

	@Autowired
	UserService userService;

	@Autowired
	RoleService roleService;

	@Autowired
	private UserSecurityService securityService;

	@RequestMapping
	public ModelAndView home() {
		ModelAndView modelAndView = null;
		List<User> users = new ArrayList<User>();
		users = userService.getAll();
		modelAndView = new ModelAndView("users/list", "users", users);
		return modelAndView;
	}

	@RequestMapping("/create")
	public ModelAndView createUser() {
		ModelAndView modelAndView = null;
		User user = new User();
		modelAndView = new ModelAndView("users/create", "user", user);
		modelAndView.addObject("roles", roleService.getAll());
		return modelAndView;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView create(@Valid User user, BindingResult result,
			ModelMap modelMap) throws UserAlreadyExistException {

		ModelAndView modelAndView = new ModelAndView("redirect:/users");
		if (result.hasErrors()) {
			modelAndView = new ModelAndView("users/create", "user", user);
			return modelAndView;
		}
		try {
			securityService.registerUser(user);
		} catch (UserAlreadyExistException e) {
			String userExist = "User with login '" + user.getLogin()
					+ "' already exists.";
			modelAndView = new ModelAndView("users/create", "user",
					user);
			modelAndView.addObject("error_register", userExist);
			modelAndView.addObject("roles", roleService.getAll());
		}
		return modelAndView;
	}

	@RequestMapping("/show/{id}")
	public ModelAndView showUser(@PathVariable("id") Long id) {
		ModelAndView modelAndView = new ModelAndView("users/show", "user", userService.findById(id));
		return modelAndView;
	}

	@RequestMapping("/edit/{id}")
	public ModelAndView editUser(@PathVariable("id") Long id) {
		ModelAndView modelAndView = new ModelAndView("users/update", "user", userService.findById(id));
		modelAndView.addObject("roles", roleService.getAll());
		return modelAndView;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(@ModelAttribute("user") User user, BindingResult result,
			ModelMap modelMap){
		ModelAndView modelAndView = null;
		if (result.hasErrors()) {
            modelAndView = new ModelAndView("users/update" , "user", user);
            return modelAndView;
        }
		securityService.updateUser(user);
		modelAndView = new ModelAndView("redirect:/users");
        return modelAndView;
	}

	@RequestMapping("/delete/{id}")
	public ModelAndView deleteUser(@PathVariable("id") Long id) {
		ModelAndView modelAndView = new ModelAndView("redirect:/users");
		userService.delete(userService.findById(id));
		return modelAndView;
	}

}
